package com.wry.es.controller;
import java.math.BigDecimal;
import java.time.LocalDateTime;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
import com.fandow.common.core.pagination.Paging;
import com.wry.es.dao.OmsOrder;
import com.wry.es.dto.OrderPageDTO;
import com.wry.es.service.OmsOrderService;
import com.wry.es.service.impl.HandleOrderService;
import com.wry.es.utils.ApiResult;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.IndexOperations;
import org.springframework.data.elasticsearch.core.document.Document;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 *<h1>  <h1>
 *
 * @author wry
 * @since 2024/9/2 14:09
 */
@RequestMapping("order")
@RestController
public class OrderController {

    @Resource
    private OmsOrderService omsOrderService;
    @Resource
    private HandleOrderService handleOrderService;
    @Resource
    private ElasticsearchOperations elasticsearchOperations;

    /**
     * 删除并创建新的索引
     */
    @GetMapping("del-and-create/index")
    public ApiResult<String> delAndCreate() {
        IndexOperations indexOps = elasticsearchOperations.indexOps(OmsOrder.class);
        if (indexOps.exists()) {
            final boolean delete = indexOps.delete();
            System.out.println(TableInfoHelper.getTableInfo(OmsOrder.class).getTableName() + " 删除索引 -> " + delete);
        }
        // 创建索引
        indexOps.create();
        // 根据实体类注解生成映射并应用
        final Document document = indexOps.createMapping();
        final boolean mappingBl = indexOps.putMapping(document);
        System.out.println("putMapping -> " + mappingBl);
        System.out.println("创建索引 -> " + TableInfoHelper.getTableInfo(OmsOrder.class).getTableName());
        return ApiResult.ok();
    }

    /**
     * 批量保存
     */
    @PostMapping("save")
    public ApiResult<String> saveOrders(@RequestBody List<OmsOrder> omsOrderList) {
        handleOrderService.save(omsOrderList);
        return ApiResult.success("ok");
    }

    /**
     * 批量保存
     */
    @PostMapping("page")
    public ApiResult<Paging<OmsOrder>> saveOrders(@RequestBody OrderPageDTO pageDTO) {
        return ApiResult.success(handleOrderService.pageList(pageDTO));
    }
}
